package com.acce.gxsc.page.inCarRecord;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.Spinner;
import android.widget.TextView;

import com.acce.gxsc.R;
import com.acce.gxsc.common.App;
import com.acce.gxsc.common.DateUtils;
import com.acce.gxsc.common.FullyLinearLayoutManager;
import com.acce.gxsc.common.Tools;
import com.acce.gxsc.model.netBean.InCarBean;
import com.acce.gxsc.page.inCarDetail.InCarActivity;
import com.acce.sdk.BaseActivity;
import com.acce.sdk.User;
import com.acce.sdk.base.mvp.BaseMvpFragment;
import com.acce.sdk.widget.adapterrv.CommonAdapter;
import com.acce.sdk.widget.adapterrv.ViewHolder;

import java.text.MessageFormat;
import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by huxley on 2017/5/26.
 */
public class InCarRecordFragment extends BaseMvpFragment<InCarRecordView, InCarRecordPresenter> implements InCarRecordView {

    View rootView;
    @Bind(R.id.rv_goods_prediction)
    RecyclerView mRvGoodsPrediction;
    @Bind(R.id.tv_duty_id)
    TextView     mTvDutyId;
    @Bind(R.id.tv_gate_name)
    TextView     mTvGateName;
    @Bind(R.id.tv_real_name)
    TextView     mTvRealName;
    @Bind(R.id.spn_dilivery_type)
    Spinner      mSpnDiliveryType;
    @Bind(R.id.tv_print)
    TextView     mTvPrint;

    CommonAdapter<InCarBean> mCommonAdapter;
    ArrayList<InCarBean>     data;
    ArrayList<String>        mDiliveryTypeData;
    LayoutInflater           mInflater;
//    @Bind(R.id.tv_father)
//    EditText           mEtFather;
//    @Bind(R.id.tv_son)
//    EditText           mEtSon;
//    @Bind(R.id.swipeRefreshLayout)
//    SwipeRefreshLayout mSwipeRefreshLayout;

    public static InCarRecordFragment newInstance() {
        Bundle args = new Bundle();
        InCarRecordFragment fragment = new InCarRecordFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected InCarRecordPresenter createPresenter() {
        return new InCarRecordPresenter();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_in_car_record, container, false);
        ButterKnife.bind(this, rootView);
        init();
        mPresenter.loadInCarList(null);
        return rootView;
    }

    private void init() {
        data = new ArrayList<>();
        mInflater = LayoutInflater.from(getActivity());

        mDiliveryTypeData = new ArrayList<>();
        mDiliveryTypeData.add("全部登记类型");
        mDiliveryTypeData.add("整柜-私人柜");
        mDiliveryTypeData.add("整柜-月结公司");

        mTvDutyId.setText(Tools.getString(App.dutyId));
        mTvGateName.setText(Tools.getString(App.gateName));
        mTvRealName.setText(Tools.getString(User.getInstance().getRealName()));

        mSpnDiliveryType.setAdapter(new com.acce.sdk.widget.adapterlv.CommonAdapter<String>(getContext(), R.layout.input_spinner_item_1, mDiliveryTypeData) {
            @Override
            protected void convert(com.acce.sdk.widget.adapterlv.ViewHolder viewHolder, String s, int i) {
                viewHolder.setText(R.id.tv_content, s);
            }
        });
        mRvGoodsPrediction.setLayoutManager(new LinearLayoutManager(getContext()));
        mRvGoodsPrediction.setAdapter(mCommonAdapter = new CommonAdapter<InCarBean>(getContext(), R.layout.in_car_record_item_car, data) {
            @Override
            protected void convert(ViewHolder holder, final InCarBean saleSheet, final int position) {
                holder.setText(R.id.tv_id, String.valueOf(position + 1));
                holder.setText(R.id.tv_acce_temp_1, Tools.getString(saleSheet.acceTemp1));
                holder.setText(R.id.tv_plate_num, Tools.getString3(saleSheet.plateNum, saleSheet.containNo, saleSheet.hangNum));
                holder.setText(R.id.tv_contact_name, Tools.getString4(saleSheet.contactName, saleSheet.underCompany));
                holder.setText(R.id.tv_create_time, Tools.getTime(saleSheet.createTime));

                holder.setOnClickListener(R.id.rl_content, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(InCarRecordFragment.this.getActivity(), InCarActivity.class);
                        intent.putExtra("inCarKey", saleSheet.inCarKey);
                        InCarRecordFragment.this.getActivity().startActivityForResult(intent, 1001);
                    }
                });
            }
        });

        mSpnDiliveryType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                int selectedItemPosition = mSpnDiliveryType.getSelectedItemPosition();
                mPresenter.loadInCarList(selectedItemPosition == 0 ? null : mDiliveryTypeData.get(selectedItemPosition));
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode != Activity.RESULT_OK) {
            return;
        }
        switch (requestCode) {
            case 1001:
                mPresenter.loadInCarList(null);
                break;
        }
    }

    @Override
    public void showProgressDialog(String s, boolean b) {
        ((BaseActivity) getActivity()).showProgressDialog(s, b);
    }

    @Override
    public void hideProgressDialog() {
        ((BaseActivity) getActivity()).hideProgressDialog();
    }

    @Override
    public void loadRegisterCarInfoSuccess(String datas) {

    }

    @Override
    public void loadInCarListSuccess(ArrayList<InCarBean> inCarList) {
        mCommonAdapter.getDatas().clear();
        if (inCarList != null) {
            mCommonAdapter.getDatas().addAll(inCarList);
        }
        mCommonAdapter.notifyDataSetChanged();
    }

    @Override
    public void printSuccess() {
    }

    @OnClick(R.id.tv_print)
    public void onClick() {
        mTvPrint.setBackgroundColor(Tools.getColor(R.color.color_e4e4e4));
        mTvPrint.setEnabled(false);
        mTvPrint.setText("请稍等...");
        ScrollView rootView = (ScrollView) mInflater.inflate(R.layout.in_car_record_dialog_print, (ViewGroup) getActivity().findViewById(R.id.dialog));
        final LinearLayout llContent = ButterKnife.findById(rootView, R.id.ll_content);
        final TextView tvClasses = ButterKnife.findById(rootView, R.id.tv_classes);
        final TextView tvCount = ButterKnife.findById(rootView, R.id.tv_count);
        final RecyclerView rv = ButterKnife.findById(rootView, R.id.rv);
        final TextView tvTollCollector = ButterKnife.findById(rootView, R.id.tv_toll_collector);
        final TextView tvDate = ButterKnife.findById(rootView, R.id.tv_date);
        final TextView tvDiliveryType = ButterKnife.findById(rootView, R.id.tv_dilivery_type);
        tvClasses.setText(MessageFormat.format("班次：{0}", App.onTime));
        tvCount.setText(MessageFormat.format("总进场车辆：{0}车次", mCommonAdapter.getDatas().size()));
        tvDiliveryType.setText(MessageFormat.format("送货分类：{0}", mDiliveryTypeData.get(mSpnDiliveryType.getSelectedItemPosition())));
        tvTollCollector.setText(MessageFormat.format("操作员：{0}", User.getInstance().getRealName()));
        tvDate.setText(DateUtils.getTime1());

        rv.setLayoutManager(new FullyLinearLayoutManager(getContext()));
        rv.setNestedScrollingEnabled(false);
        rv.setAdapter(new CommonAdapter<InCarBean>(getContext(), R.layout.in_car_record_dialog_print_item_car, mCommonAdapter.getDatas()) {
            @Override
            protected void convert(ViewHolder holder, final InCarBean saleSheet, final int position) {
                holder.setText(R.id.tv_id, String.valueOf(position + 1));
                holder.setText(R.id.tv_acce_temp_1, Tools.getString(saleSheet.acceTemp1));
                holder.setText(R.id.tv_plate_num, Tools.getString3(saleSheet.plateNum, saleSheet.containNo, saleSheet.hangNum));
                holder.setText(R.id.tv_contact_name, Tools.getString4(saleSheet.contactName, saleSheet.underCompany));
                holder.setText(R.id.tv_create_time, Tools.getTime(saleSheet.createTime));
            }
        });
        final AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
        final AlertDialog alertDialog = builder
//                .setTitle("是否打印交接班汇总报表")
                .setView(rootView)
                .setNegativeButton("取消", null)
                .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        mPresenter.printOrder(llContent);
                        mTvPrint.setText("打印成功");
                    }
                }).create();
        alertDialog.setCanceledOnTouchOutside(false);
        alertDialog.show();
    }
}
